package com.example.smartcity.ui.dashboard;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.StopMessageBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class StopCarMessage extends AppCompatActivity {

    private TextView TVParkName;
    private TextView TVDistanceStop;
    private TextView TVStoppriceCaps;
    private TextView TVStopaddress;
    private TextView TVStopRates;
    private TextView TVStopvacancy;
    private String TAG = "TAG";
    StopMessageBean bean;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    StopMessageBean.DataBean dataBean = bean.getData();
                    TVParkName.setText(dataBean.getParkName());
                    TVDistanceStop.setText(dataBean.getDistance()+"公里");
                    TVStoppriceCaps.setText("每小时"+dataBean.getRates()+"元,"+"最高"+dataBean.getPriceCaps()+"元一天");
                    TVStopaddress.setText(dataBean.getAddress());
                    TVStopRates.setText(dataBean.getRates()+"/小时");
                    TVStopvacancy.setText(dataBean.getVacancy()+"个/"+dataBean.getAllPark());
                    break;
            }

            super.handleMessage(msg);
        }
    };
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_car_message);
         TVParkName = findViewById(R.id.TV_parkName);
         TVDistanceStop = findViewById(R.id.TV_distanceStop);
         TVStoppriceCaps = findViewById(R.id.TV_StoppriceCaps);
         TVStopaddress = findViewById(R.id.TV_Stopaddress);
         TVStopRates = findViewById(R.id.TV_StopRates);
         TVStopvacancy = findViewById(R.id.TV_Stopvacancy);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        Intent intent = getIntent();
        int id = intent.getIntExtra("id",0);
        if (id!=0){
            String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/parklot/"+id;
            OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.d("TAG", "onFailure: "+e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Gson gson = new Gson();
                    bean =gson.fromJson(response.body().string(),StopMessageBean.class);
                    if (bean.getCode()==200){
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    }
                }
            });
        }


    }
}